<?php
require_once dirname(__FILE__) . '/common/common.inc.php';
require_once dirname(__FILE__) . "/database/$database/database.inc.php";
  
header('Content-Type: text/plain');

try {
  $hostname = DB_HOST;
  $username = DB_LOGIN;
  $password = DB_PASS;
  $dbname = DB_NAME;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo ERROR_SIGN . $e->getMessage();
}

$q = json_decode(MiscUtils::getParam('q', NULL));
try {
  $myPdo->beginTransaction();
  foreach ($q as &$tmp) {
    $orm = classToOrm($tmp->n);
    if ($orm) {
      foreach ($tmp->a as &$obj) {
        if (isset($obj->id)) {
          $myPdo->exec(MiscUtils::obj2Upd($tmp->n, $obj));
        } else {
          $myPdo->exec(MiscUtils::obj2Ins($tmp->n, $obj));
        }
      }
      foreach ($tmp->r as &$obj) {
        $myPdo->exec(MiscUtils::obj2Del($tmp->n, $obj));
      }
    }
  }
  $myPdo->commit();
  echo json_encode(array());
} catch (PDOException $e) {
  $myPdo->rollBack();
  echo ERROR_SIGN . $e->getMessage();
}
?>